from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from story.models import Story
from django.utils.html import strip_tags


class Notification(models.Model):
    NOTIFICATION_TYPES = [
        ('like', '点赞'),
        ('comment', '评论'),
        ('follow', '关注'),
        ('system', '系统'),
    ]

    recipient = models.ForeignKey(User, on_delete=models.CASCADE, related_name='received_notifications',
                                  verbose_name='接收者')
    sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sent_notifications', null=True, blank=True,
                               verbose_name='发送者')
    notification_type = models.CharField(max_length=20, choices=NOTIFICATION_TYPES, verbose_name='通知类型')
    story = models.ForeignKey(Story, on_delete=models.CASCADE, null=True, blank=True, related_name='notifications',
                              verbose_name='相关故事')
    content = models.TextField(verbose_name='通知内容')
    is_read = models.BooleanField(default=False, verbose_name='已读状态')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    related_story = models.ForeignKey(Story, on_delete=models.CASCADE, null=True, blank=True,
                                      related_name='related_notifications', verbose_name='相关故事')

    class Meta:
        verbose_name = '通知'
        verbose_name_plural = '通知'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.get_notification_type_display()}通知 - {self.recipient.username}'


class Follow(models.Model):
    follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name='following', verbose_name='关注者')
    following = models.ForeignKey(User, on_delete=models.CASCADE, related_name='followers', verbose_name='被关注者')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='关注时间')

    class Meta:
        verbose_name = '关注'
        verbose_name_plural = '关注'
        unique_together = ['follower', 'following']
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.follower.username}关注了{self.following.username}'


class Report(models.Model):
    REPORT_TYPES = [
        ('spam', '垃圾信息'),
        ('inappropriate', '不当内容'),
        ('copyright', '版权问题'),
        ('other', '其他'),
    ]

    reporter = models.ForeignKey(User, on_delete=models.CASCADE, related_name='reports', verbose_name='举报者')
    story = models.ForeignKey(Story, on_delete=models.CASCADE, related_name='reports', verbose_name='被举报故事')
    report_type = models.CharField(max_length=20, choices=REPORT_TYPES, verbose_name='举报类型')
    description = models.TextField(verbose_name='举报描述')
    is_handled = models.BooleanField(default=False, verbose_name='已处理')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='举报时间')

    class Meta:
        verbose_name = '举报'
        verbose_name_plural = '举报'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.reporter.username}举报了{self.story.title}'


class Like(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    story = models.ForeignKey(Story, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('user', 'story')
        ordering = ['-created_at']


class Favorite(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    story = models.ForeignKey(Story, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('user', 'story')
        ordering = ['-created_at']


class Comment(models.Model):
    STATUS_CHOICES = [
        ('pending', '待审核'),
        ('approved', '已通过'),
        ('rejected', '已拒绝'),
    ]
    
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='评论用户')
    story = models.ForeignKey(Story, on_delete=models.CASCADE, related_name='comments', verbose_name='评论故事')
    content = models.TextField(verbose_name='评论内容')
    parent_comment = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, 
                                     related_name='replies', verbose_name='父评论')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='审核状态')
    rejected_reason = models.TextField(blank=True, null=True, verbose_name='拒绝原因')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        ordering = ['-created_at']
        verbose_name = '评论'
        verbose_name_plural = '评论'

    def __str__(self):
        return f'{self.user.username} 对 {self.story.title} 的评论'

    def get_content_safe(self):
        """返回没有HTML标签的评论内容"""
        return strip_tags(self.content)

    @property
    def is_reply(self):
        """判断是否为回复评论"""
        return self.parent_comment is not None
        
    @property
    def is_approved(self):
        """判断评论是否已审核通过"""
        return self.status == 'approved'
        
    @property
    def is_pending(self):
        """判断评论是否待审核"""
        return self.status == 'pending'
        
    @property
    def is_rejected(self):
        """判断评论是否被拒绝"""
        return self.status == 'rejected'


class CommentLike(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comment_likes')
    comment = models.ForeignKey(Comment, on_delete=models.CASCADE, related_name='likes')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('user', 'comment')
        ordering = ['-created_at']
        
    def __str__(self):
        return f'{self.user.username} 点赞了 {self.comment.user.username} 的评论'
